import {computed, onMounted, reactive, type Ref, ref} from "vue";

/**
 * @Author: wangjian
 * 656876071@qq.com
 * @Date: 2024-07-19 11:40:23
 * @LastEditTime: 2024-09-11 13:51:01
 * @FilePath: src/views/sxln/member/LnMemberHooks.ts
 * @Description:
 */
import {lnMemberApi, type LnMemberCase} from "@/views/sxln/api/LnMemberApi"
import type {Dayjs} from 'dayjs'
import {message, type UploadFile} from "ant-design-vue";
import {fileStorageApi} from '@/core/api/sys/FileStorageApi'
import {type UploadRequestOption} from "ant-design-vue/lib/vc-upload/interface";
import type {Ret} from "@/core/utils/request";
import {useQueryTableV1} from "@/core/common/hooks/TableHooks";

export const useEntrance = () => {
    const columns: Array<any> = [
        {
            title: '姓名',
            dataIndex: 'realName',
            align:'center',
        },
        {
            title: '会员证编号',
            dataIndex: 'memberId',
            align:'center',
        },
        {
            title: '笔名',
            dataIndex: 'penName',
            align:'center',
        },
        {
            title: '性别',
            dataIndex: 'sex',
            align:'center',
            dict:'base_sex'
        },
        {
            title: '会员类别',
            dataIndex: 'memberType',
            dict:'member_type'
        },
        {
            title: '入会时间',
            dataIndex: 'entranceTime',
        },
        {
            title: '操作',
            dataIndex: 'operate'
        }
    ]
    
    const {getMemberPage, deleteMemberById} = lnMemberApi()
    const params = reactive<LnMemberCase>({})
    // const queryTable = useQueryTable(getMemberPage, deleteMemberById, params,false )
    const queryTable = useQueryTableV1({queryPageApi:getMemberPage, deleteByIdApi:deleteMemberById, params, runDefaultMounted:false})
    return {
        params,
        columns,
        ...queryTable
    }
}

/**
 * @Author: wangjian
 * 详情
 */
export const useDetail = () => {
    const entranceTimePicker = ref<Dayjs>()

    /**
     * 上传
     */
    const {uploadFile,getStraightChain,previewAddr} = fileStorageApi()
    const photoObj = ref<any>({})
    const previewPrefix = ref('')
    const fileList = ref([])
    
    onMounted(() => {
        previewAddr().then(ret=>{
            previewPrefix.value = ret.data
        })
    })
    
    const preview = computed(()=>{
        return previewPrefix.value  + photoObj.value.url
    })
    
    const
        uploadPhoto = {
            handleChange:
                ({file}: { file: UploadFile }) => {
                    console.log(file.status)
                    if (file.status === 'done') {
                        message.success(`${file.name} 上传成功`)
                    } else if (file.status === 'error') {
                        message.error(`${file.name} 上传失败`)
                    } else if (file.status === 'uploading') {
                        message.info(`${file.name} 正在上传`)
                    }
                },
            beforeUpload:
                (file: File) => {
                    if (file.size > 1024 * 500) {
                        message.error(`${file.name} 照片大小不能超过500kb`)
                        return false
                    }
                    return true
                },
            upload:
                (e: UploadRequestOption) => {
                    uploadFile(e.file as UploadFile).then((res: any | Ret) => {
                        if (res.code != 0) {
                            throw res.data
                        }
                        console.log("上传完成", res)
                        // form.value.photo = res.data.id
                        photoObj.value = res.data
                        message.success(`上传成功`)
                        return res.data.id
                    }).then(id=>{
                        getStraightChain(id).then((ret:any)=>{
                            if(ret.code!=0){
                                throw ret.data
                            }
                            photoObj.value.url = ret.data
                        })
                    })
                }
        }
    /**
     * 上传照片
     */

    return {
        entranceTimePicker,
        uploadPhoto,
        fileList,
        photoObj,
        preview
    }
}
